package com.block.channelext.service.channel;

import cn.hutool.core.net.URLEncodeUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.channelext.model.result.ReportResult;
import com.block.channelext.service.IChannelHandleService;
import com.block.common.service.IApplicationInfoService;
import com.block.common.service.LogFactory;
import com.block.db.entity.ApplicationInfo;
import com.block.db.entity.ChannelInfo;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class AiTuiFlowChannelHandleService implements IChannelHandleService {

    private String requestUrl = "http://engine.aituihd.com/st/effect";

    private static Logger log = LogFactory.getCommLog();

//    private String url = "https://h5.longzankeji.cn/#/?ch=9NMY0E";
//    private String firstUrl = "https://ext.longzankeji.cn/exp/chl/9NMY0E";

    @Autowired
    IApplicationInfoService applicationInfoService;

    @Override
    public ReportResult registerAction(ChannelInfo channelInfo, JSONObject extJson) {

        String ccbid = extJson.getString("ccbid");
        if (StrUtil.isEmpty(ccbid)){
            return ReportResult.newInstance();
        }

        ApplicationInfo appInfoByType = applicationInfoService.getAppInfoByType(channelInfo.getAppType());
        String  firstUrl = StrUtil.format("{}/exp/chl/{}", appInfoByType.getExtUrl(), channelInfo.getChannelCode());
        String url = StrUtil.format("{}/?ch={}&ccid={}", appInfoByType.getH5Url(), channelInfo.getChannelCode(), ccbid);

        String reqIp = extJson.getString("reqIp");

        JSONObject extData = extJson.getJSONObject("extData");
        String userAgent = null;
        if (ObjectUtil.isNotEmpty(extData)){
            userAgent = extData.getString("userAgent");
        }

        Map<String, Object> bizMap = new HashMap<>();
        bizMap.put("bid", ccbid);
        bizMap.put("act", "6");
        bizMap.put("url", URLEncodeUtil.encode(url));
        bizMap.put("firstUrl", URLEncodeUtil.encode(firstUrl));
        bizMap.put("ip", reqIp);
        bizMap.put("ua", userAgent);

        log.info("【艾推信息流上报】【注册】【开始请求】【reqestUrl】{}【bizMap】{}", requestUrl, JSON.toJSONString(bizMap));
        String response = HttpUtil.createGet(requestUrl)
                .form(bizMap)
                .execute().body();
        log.info("【艾推信息流上报】【注册】【开始结束】【result】{}", response);

        ReportResult reportResult = ReportResult.newInstance(JSON.toJSONString(bizMap), response);
        JSONObject jsonObject = JSONObject.parseObject(response);
        String code = jsonObject.getString("code");
        String message = jsonObject.getString("message");
        reportResult.setCode(code);
        reportResult.setMessage(message);
        if (StrUtil.equals(code, "0")){
            reportResult.setSuccess(true);
        }

        return reportResult;
    }

    @Override
    public ReportResult authAction(ChannelInfo channelInfo, JSONObject extJson) {

        return ReportResult.newInstance();
    }

    @Override
    public ReportResult proUvAction(ChannelInfo channelInfo, JSONObject extJson) {

        return ReportResult.newInstance();
    }

}
